﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyLibary.BIZ;
using System.Data;

namespace MyLibary.DAL
{
    class DAL_NguoiDung
    {
        private static  DAL_Connection connect = new DAL_Connection();
        #region Metholds
        internal static List<BIZ_NguoiDung> GetAll()
        {
            DataTable tb=connect.ExecuteSelect("SELECT * FROM NguoiDung");
            List<BIZ_NguoiDung>result=new List<BIZ_NguoiDung>();
            for(int i=0;i<tb.Rows.Count;i++)
                result.Add(new BIZ_NguoiDung(tb.Rows[i][0].ToString(),tb.Rows[i][1].ToString(),tb.Rows[i][2].ToString(),tb.Rows[i][3].ToString(),tb.Rows[i][4].ToString(),Convert.ToInt32(tb.Rows[i][5].ToString())));
            return result;
        }
        /// <summary>
        /// lấy thông tin người dùng thông qua tài khoản và mật khẩu
        /// </summary>
        /// <param name="TaiKhoan"></param>
        /// <param name="MatKhau"></param>
        /// <returns></returns>
        internal static BIZ_NguoiDung GetUser(string TaiKhoan,string MatKhau)
        {
            DataTable tb = connect.ExecuteSelect("SELECT * FROM NguoiDung WHERE TaiKhoan='"+TaiKhoan+"' AND MatKhau='"+MatKhau+"'");
            return new BIZ_NguoiDung(tb.Rows[0][0].ToString(), tb.Rows[0][1].ToString(), tb.Rows[0][2].ToString(), tb.Rows[0][3].ToString(), tb.Rows[0][4].ToString(), Convert.ToInt32(tb.Rows[0][5].ToString()));
        }
        internal static BIZ_NguoiDung GetUser(string TaiKhoan)
        {
            DataTable tb = connect.ExecuteSelect("SELECT * FROM NguoiDung WHERE TaiKhoan='" + TaiKhoan + "'");
            return new BIZ_NguoiDung(tb.Rows[0][0].ToString(), tb.Rows[0][1].ToString(), tb.Rows[0][2].ToString(), tb.Rows[0][3].ToString(), tb.Rows[0][4].ToString(), Convert.ToInt32(tb.Rows[0][5].ToString()));
        }
        /// <summary>
        /// thêm 1 record vào table người dùng
        /// </summary>
        /// <param name="nguoiDung"></param>
        /// <returns></returns>
        internal static bool Insert(BIZ_NguoiDung nguoiDung)
        {
            return connect.ExecuteUpdate(String.Format("INSERT INTO NguoiDung VALUES('{0}','{1}','{2}','{3}','{4}','{5}')", nguoiDung.TaiKhoan, nguoiDung.Ten, nguoiDung.MatKhau, nguoiDung.DiaChi, nguoiDung.SDT, nguoiDung.Quyen));
        }
        /// <summary>
        /// cập nhật 1 record trong table người dùng
        /// </summary>
        /// <param name="nguoiDung"></param>
        /// <returns></returns>
        internal static bool Update(BIZ_NguoiDung nguoiDung)
        {
            string query = String.Format("UPDATE NguoiDung SET Ten='{0}',MatKhau='{1}',DiaChi='{2}',SDT='{3}',Quyen='{4}' WHERE TaiKhoan='{5}'", nguoiDung.Ten, nguoiDung.MatKhau, nguoiDung.DiaChi, nguoiDung.SDT, nguoiDung.Quyen,nguoiDung.TaiKhoan);
            Console.WriteLine(query);
            return connect.ExecuteUpdate(query);
        }
        /// <summary>
        /// xóa 1 tài khoản
        /// </summary>
        /// <param name="nguoiDung"></param>
        /// <returns></returns>
        internal static bool Delete(BIZ_NguoiDung nguoiDung)
        {
            return connect.ExecuteUpdate("DELETE NguoiDung WHERE TaiKhoan='" + nguoiDung.TaiKhoan + "'");
        }
        #endregion
    }
}
